﻿package DevilCat{
	import flash.display.MovieClip ;
	import flash.text.TextField ;
	import flash.events.MouseEvent;    
	import DevilCat.BlankButtonObject;
	import DevilCat.PokemonBattle;
	import DevilCat.FloatingText;
	public class ItemObject extends MovieClip  {
		public var iid:Number;
		public var Code:String;
		public var Name:String;
		public var Stat:String;
		public var Effect:Number;
		public var Cost:Number;
		public var NumberOf:Number;
		public var Type:String;
		public function ItemObject(iiid:Number,iCode:String,iName:String,iStat:String,iEffect:Number,iCost:Number,iNumberOf:Number,iType:String="NoType") {
			iid=iiid;
			Code=iCode;
			Name=iName;
			Stat=iStat;
			Effect=iEffect;
			Cost=iCost;
			NumberOf=iNumberOf;			
			Type=iType;
			reText();
		}
		public function AddEventUseItem(){
			while (PokemonInforPanel.numChildren > 0)
			{
				PokemonInforPanel.removeChildAt(0);
			}
			for(var i:Number=0;i<SQLData.pokemonList.length;i++){
				var newBlankButton:BlankButtonObject=new BlankButtonObject(SQLData.pokemonList[i].CurrentHP,SQLData.pokemonList[i].Name);
				newBlankButton.SetData(i);
				newBlankButton.SetX(-1);
				PokemonInforPanel.addChild(newBlankButton);
			}
			PokemonInforPanel.visible=false;
			if(Type=="Ball"){
				this.addEventListener(MouseEvent.CLICK, clickBall);				
			}else{
				PokemonInforPanel.addEventListener(MouseEvent.CLICK, clickItem);
				this.addEventListener(MouseEvent.MOUSE_OVER,showInfor);
				this.addEventListener(MouseEvent.MOUSE_OUT,hideInfor);	
			}
		}
		public function AddEventBuyItem(){		
			this.addEventListener(MouseEvent.CLICK, clickBuyItem);			
		}
		public function clickBuyItem(evt:MouseEvent) {
			if(SQLData.pmp>=Cost){
				addChild(new FloatingText("+1",100,0));
				SQLData.changePMP(-Cost);
				SQLData.AddItem(this);			
			}else{				
				addChild(new FloatingText("BAN KHONG DU PMP",100,0));				
			}
		}
		public function clickBall(evt:MouseEvent) {
				if (NumberOf>0) {	
					if(this.parent.parent.toString()=="[object PokemonBattle]"){
						if(!SQLData.battleClip.MyPokemon.UsingItem && SQLData.battleClip.TargetPokemon.pokemon.CurrentHP>0 ){
							SQLData.battleClip.ThrowBall(Name);
							SQLData.pokemonList[0].AttackMC.DelayUseItem(5);
							NumberOf--;	
							SQLData.UpdateItemSQL(this,-1);							
						}
					}
				}
		}
		public function clickItem(evt:MouseEvent) {
			var isUsed:Boolean=false;
			if (evt.target.bid!=undefined) {
				if (NumberOf>0) {	
				//In Battle
					if(this.parent.parent.toString()=="[object PokemonBattle]"){
						if(!SQLData.battleClip.MyPokemon.UsingItem){
							switch (Type){
								case "Potion":
									evt.target.SetContent(SQLData.pokemonList[evt.target.bid].CurrentHP,SQLData.pokemonList[evt.target.bid].Name);	
									SQLData.pokemonList[evt.target.bid].UseItem(this);
									isUsed=true;
									break;
								case "XStat":
									if(SQLData.pokemonList[evt.target.bid].AttackMC){
										SQLData.pokemonList[evt.target.bid].AttackMC.ChangeStats(Stat,Effect,false);
										NumberOf--;
										SQLData.UpdateItemSQL(this,-1);
										isUsed=true;
									}
									break;
							}
							if(isUsed){
								SQLData.battleClip.MyPokemon.DelayUseItem(3);
								addChild(new FloatingText("evt.target.bid "+evt.target.bid,200,0));									
							}
						}//						if(SQLData.pokemonList[evt.target.bid].AttackMC){
//							if(!SQLData.pokemonList[evt.target.bid].AttackMC.UsingItem){
//								switch (Type){
//									case 1://"HP"
////										SQLData.pokemonList[evt.target.bid].AttackMC.DelayUseItem(3);
////										SQLData.pokemonList[evt.target.bid].UseItem(this);
////										evt.target.SetContent(SQLData.pokemonList[evt.target.bid].CurrentHP,SQLData.pokemonList[evt.target.bid].Name);	
////										addChild(new FloatingText("evt.target.bid "+evt.target.bid,200,0));
//										break;
////									case 2://"Ball"
////										SQLData.battleClip.ThrowBall(Name);
////										SQLData.pokemonList[evt.target.bid].AttackMC.DelayUseItem(8);
////										NumberOf--;
////										break;
//								}								
//							}
//						}
				//Out Battle
					}else{
						switch (Type){
							case "AddStat":
							case "Potion":
								SQLData.pokemonList[evt.target.bid].UseItem(this);
								evt.target.SetContent(SQLData.pokemonList[evt.target.bid].CurrentHP,SQLData.pokemonList[evt.target.bid].Name);		
								break;
						}
					}
					reText();
				} else {
					addChild(new FloatingText("KHONG CON ITEM",200,0));
				}
			}
		}
		private function showInfor(e:MouseEvent) {
			PokemonInforPanel.visible=true;
			bgButton.width=200;
			bgButton.height=SQLData.pokemonList.length*24;			
		}
		private function hideInfor(e:MouseEvent) {
			PokemonInforPanel.visible=false;
			bgButton.width=1;
			bgButton.height=1;
		}
		public function reText(){
			TName.text=Name;
			TNumber.text=String(NumberOf);	
		}
	}
}